﻿using System;
using System.Collections.Generic;
using System.Linq;
using Norm;
using Norm.BSON.DbTypes;

namespace MongoDBConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var connString = "mongodb://139.196.54.102:27017";

            using (var db = Mongo.Create(connString))
            {
                /*var logCollection = db.GetCollection<SystemLog>();


                for (int i = 0; i < 10000; i++)
                {
                    logCollection.Save(new SystemLog()
                    {
                        AppName = "woooodi.mall",
                        LogType = (LogType)(new Random(Guid.NewGuid().GetHashCode()).Next(0, 4)),
                        ClassName = "ClassName" + new Random(Guid.NewGuid().GetHashCode()).Next(0, 10),
                        MethodName = "MethodName" + new Random(Guid.NewGuid().GetHashCode()).Next(0, 10),
                        Content = "Content" + i,
                        AddTime = DateTime.Now
                    });
                }*/
            }

            using (var db = Mongo.Create(connString))
            {
                var queryable = db.GetCollection<SystemLog>().AsQueryable().Where(item => item.LogType == LogType.Debug);

                var count = db.GetCollection<SystemLog>().AsQueryable().Count(item => item.LogType == LogType.Debug);

                Console.WriteLine("Total Count： {0}", count);

                var pageSize = 10;

                for (int i = 1; i < count / pageSize; i++)
                {
                    var logs = queryable.Skip(pageSize * i).Take(pageSize).ToList();

                    Console.WriteLine("Current PageIndex： {0}", i);

                    foreach (var log in logs)
                    {
                        Console.WriteLine("Available: {0} {1} {2}", log.LogType, log.Content, log.AddTime);
                    }
                }
            }

            Console.Read();
        }
    }

    public class SystemLog
    {
        public ObjectId _id { get; set; }

        public string AppName { get; set; }

        public LogType LogType { get; set; }

        public string ClassName { get; set; }

        public string MethodName { get; set; }

        public DateTime AddTime { get; set; }

        public string Ip { get; set; }

        public string Content { get; set; }
    }

    public enum LogType
    {
        Error,
        Info,
        Debug,
        Warn
    }
}
